package com.jzr.medical.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzr.common.entity.vo.Result;
import com.jzr.medical.db1.dto.ModelData2;
import com.jzr.medical.db1.service.PdaPageControlService;
import com.jzr.medical.db1.service.PdaPageOptionService;
import com.jzr.medical.db1.vo.PdaPageControlVo;
import com.jzr.medical.db1.vo.PdaPageOptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/pdaPageControl")
public class PdaPageControlController {
    @Autowired
    private PdaPageControlService pdaPageControlService;

    @Autowired
    private PdaPageOptionService pdaPageOptionService;

    @GetMapping("/list")
    public Result<List<PdaPageControlVo>> list(PdaPageControlVo record){
        QueryWrapper<PdaPageControlVo> qw = new QueryWrapper<>();
        qw.orderByAsc("sort");
        qw.eq(PdaPageControlVo.PageId_,record.getPageId());
        List<PdaPageControlVo> list = pdaPageControlService.list(qw);

        return Result.success("成功",list);
    }
    @GetMapping("/listControlAndOption")
    public Result<Map<String,Object>> listControlAndOption(Integer pageId){
        if(pageId == null){
            return Result.fail("参数pageId不能为空");
        }
        QueryWrapper<PdaPageControlVo> cqw = new QueryWrapper<>();
        cqw.orderByAsc("sort");
        cqw.eq(PdaPageControlVo.PageId_,pageId);
        List<PdaPageControlVo> controlList = pdaPageControlService.list(cqw);

        QueryWrapper<PdaPageOptionVo> pqw = new QueryWrapper<>();
        pqw.orderByAsc("sort");
        pqw.eq(PdaPageOptionVo.PageId_,pageId);
        List<PdaPageOptionVo> optionList = pdaPageOptionService.list(pqw);

        Map<String,Object> map = new HashMap<>();
        map.put("controlList",controlList);
        map.put("optionList",optionList);

        return Result.success("成功",map);
    }
    @PostMapping("/saveList")
    public Result<List<PdaPageControlVo>> saveList(@RequestBody List<PdaPageControlVo> list){
        List<PdaPageControlVo>  result = pdaPageControlService.saveBatch(list);
        return Result.success("成功",result);
    }
    @PostMapping("/saveBatch")
    public Result<ModelData2> saveBatch(@RequestBody ModelData2<List<PdaPageControlVo>, List<PdaPageOptionVo>> record){
        ModelData2 result = new ModelData2<>();
        result.setData1(pdaPageControlService.saveBatch(record.getData1()));
        result.setData2(pdaPageOptionService.saveBatch(record.getData2()));

        return Result.success("成功",result);
    }

}
